import { DocumentEventCenter } from "../base_frame/DocumentEventCenter";
import { IShaderBase } from "../base_frame/IShaderBase";
import { ShaderBase } from "../base_frame/ShaderBase";
import { ShaderMgr } from "../base_frame/ShaderMgr";

export class TextureCube extends ShaderBase implements IShaderBase {

    protected readonly VS: string =
        "attribute vec4 a_Position;\n" +
        "attribute vec4 a_Color;\n" +
        "attribute vec2 a_TexCoord;\n" +

        "uniform mat4 u_MvpMatrix;\n" +
        "varying vec4 v_Color;\n" +
        "varying vec2 v_TexCoord;\n" +
        "void main(){\n" +
        "   v_Color = a_Color;\n" +
        "   v_TexCoord = a_TexCoord;\n" +
        "   gl_Position = u_MvpMatrix * a_Position;\n" +
        "}\n";

    protected readonly FS: string =
        "#ifdef GL_ES\n" +
        "precision mediump float;\n" +
        "#endif\n" +
        "uniform sampler2D u_Sampler;\n" +
        "varying vec2 v_TexCoord;\n" +
        "void main(){\n" +
        "   gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n" +
        "}\n";



    protected a_Position: number;
    protected a_TexCoord: number;

    protected u_Sampler: WebGLUniformLocation;

    protected count: number;
    cubeTexture: WebGLTexture;
    indiceBuffer: WebGLBuffer;
    posBuffer: WebGLBuffer;
    coordBuffer: WebGLBuffer;

    init(gl: WebGL2RenderingContext | WebGLRenderingContext): void {
        this.program = createProgram(gl, this.VS, this.FS);
        if (!this.program) return console.error("创建渲染对象失败");
        //@ts-ignore
        gl.program = this.program;
        gl.useProgram(this.program);
        this.initRef(gl);
        this.count = this.initBuffer(gl);
        this.initValue(gl);
        ShaderMgr.clearColor.setValue(0.2, 0.2, 0.4);
    }
    initValue(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        if (this.u_Sampler) {
            let img = new Image();
            img.onload = (ev: any) => {
                debugger;
                gl.useProgram(this.program);
                let texture = gl.createTexture();
                if (!texture) return console.error("create texture fail!!!!!!!!!!");
                gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1); // 反转y轴
                gl.bindTexture(gl.TEXTURE_2D, texture);
                gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
                gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);
                gl.uniform1i(this.u_Sampler, 0);
                gl.bindTexture(gl.TEXTURE_2D, null);
                this.cubeTexture = texture;
            }
            img.src = "../../res/img/bizidis.jpg";
        }
    }
    initBuffer(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        //顶点坐标
        //      L4------L5 
        //      /|       /|
        //    L0------L1  |
        //     | |     |  |
        //     | L6----|-L7 
        //     | /     | /
        //    L2------L3 
        var vertices = new Float32Array([   // Vertex coordinates
            1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1,    // v0-v1-v2-v3 front
            1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1,    // v0-v3-v4-v5 right
            1, 1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1,    // v0-v5-v6-v1 up
            -1, 1, 1, -1, 1, -1, -1, -1, -1, -1, -1, 1,    // v1-v6-v7-v2 left
            -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1,    // v7-v4-v3-v2 down
            1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1     // v4-v7-v6-v5 back
        ]);

        // // Texture coordinates
        // var texCoords = new Float32Array([
        //     1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v0-v1-v2-v3 front
        //     0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0,    // v0-v3-v4-v5 right
        //     1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0,    // v0-v5-v6-v1 up
        //     1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v1-v6-v7-v2 left
        //     0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0,    // v7-v4-v3-v2 down
        //     0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0     // v4-v7-v6-v5 back
        // ]);
        // Texture coordinates
        var texCoords = new Float32Array([
            1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v0-v1-v2-v3 front
            1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v0-v1-v2-v3 front
            1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v0-v1-v2-v3 front
            1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v0-v1-v2-v3 front
            1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0,    // v0-v1-v2-v3 front
            1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0   // v0-v1-v2-v3 front
        ]);

        var indices = new Uint8Array([       // Indices of the vertices
            0, 1, 2, 0, 2, 3,    // front
            4, 5, 6, 4, 6, 7,    // right
            8, 9, 10, 8, 10, 11,    // up
            12, 13, 14, 12, 14, 15,    // left
            16, 17, 18, 16, 18, 19,    // down
            20, 21, 22, 20, 22, 23     // back
        ]);
        if (!(this.posBuffer = this.createBuffer(gl, this.a_Position, 3, vertices, gl.FLOAT))) return -1;
        if (!(this.coordBuffer =this.createBuffer(gl, this.a_TexCoord, 2, texCoords, gl.FLOAT))) return -1; // uv坐标使用的是两个数据表示一个点
        let buffer = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
        this.indiceBuffer = buffer;
        gl.bindBuffer(gl.ARRAY_BUFFER, null);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
        return indices.length;
    }

    protected createBuffer(gl: WebGL2RenderingContext | WebGLRenderingContext, index: number, size: number, data: any, type: number): WebGLBuffer {
        let buffer = gl.createBuffer();
        if (!buffer) return buffer;
        gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
        gl.bufferData(gl.ARRAY_BUFFER, data, gl.STREAM_DRAW);
        gl.vertexAttribPointer(index, size, type, false, 0, 0);
        gl.enableVertexAttribArray(index);
        return buffer;
    }

    initRef(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        this.a_Position = gl.getAttribLocation(this.program, "a_Position");
        this.a_TexCoord = gl.getAttribLocation(this.program, "a_TexCoord");
        this.u_Sampler = gl.getUniformLocation(this.program, "u_Sampler");
    }

    protected resetBuffer(gl: WebGL2RenderingContext | WebGLRenderingContext,buffer:WebGLBuffer, index: number, size: number, type: number){
        gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
        gl.vertexAttribPointer(index, size, type, false, 0, 0);
        gl.enableVertexAttribArray(index);
    }


    inputBlur(text: string): void {

    }

    draw(gl: WebGL2RenderingContext | WebGLRenderingContext): void {
        if (this.count && this.cubeTexture) {
            gl.clearColor(1.0, 0.2, 0.4, 1.0);
            gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
            gl.viewport(0,0,256,256);
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,this.indiceBuffer);
            this.resetBuffer(gl, this.posBuffer, this.a_Position, 3, gl.FLOAT);
            this.resetBuffer(gl, this.coordBuffer, this.a_TexCoord, 2, gl.FLOAT);

            gl.activeTexture(gl.TEXTURE0);
            gl.bindTexture(gl.TEXTURE_2D, this.cubeTexture);
            gl.drawElements(gl.TRIANGLES, this.count, gl.UNSIGNED_BYTE, 0);
            // 让他循环转起来
            this.moduleMatrix.rotate(1, 0, 1, 0);
            DocumentEventCenter.sendEvent(DocumentEventCenter.updateRenderFlag);
        }
    }


}